<?php
	error_reporting(0);
	session_start();
	if(!session_is_registered(myusername)){
		header("location:main_login.php");
	}
	$dbconn = pg_connect ("host=localhost port=5432 dbname=postgres user=postgres password=leliput93");
	if (!$dbconn) {
		echo "An error occured.\n";
		exit;
	}
	
	$sign = false;
	
	extract($_POST);
	$date = $year1."-".$month1."-".$day1;
	if ($submit) {
		$kode_b= pg_query($dbconn, "select kode_pembelian from supermarket.pembelian where kode_pembelian='$kode_beli'");
		$count=pg_num_rows($kode_b);
		$_SESSION['kodebeli'] = $kode_beli;
		
		if($count==0 && $kode_beli!=""&& strlen($kode_beli)==5){
			if (preg_match("/[^a-zA-Z0-9]/",$kode_beli)) {
				$err="<br/><label style=color:#FF0000> kode pembelian harus angka atau huruf!!</label>";
			}
			else {
				$sign=true;
				$res = pg_query($dbconn, "select kode from supermarket.pemasok where nama = '$pemasok'");
				$kodepemasok = pg_fetch_row($res);
				$insert_pembelian = pg_query($dbconn, "insert into supermarket.pembelian values
												   ('$_SESSION[kodebeli]','$date','$kodepemasok[0]', '$_SESSION[kodecabang]')");
				$temp_table = pg_query($dbconn, "create table supermarket.temp (
												produk varchar(25),
												kuantitas int,
												subtotal int
												)");
			}
		}
		else if($count!=0){
			$err="<br/><label style=color:#FF0000> kode pembelian sudah ada!!</label>";
		}
		else if($kode_beli==""){
			$err="<br/><label style=color:#FF0000> kode pembelian belum dimasukkan!!</label>";
		}
		else if(strlen($kode_beli)<5){
			$err="<br/><label style=color:#FF0000> kode pembelian harus 5 karakter!!</label>";
		}
	}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

	<head>
		<title>Supermarket</title>
		<meta http-equiv="content-type" content="text/html; charset=utf-8" />
		<link rel="stylesheet" href="style.css" type="text/css" />
		<script type="text/javascript" src = "tgl.js"></script>
	</head>

	<body>
	    
		<div id="container" class="clearfix">
			<h1>HEMART B10<em> CABANG <?php echo " $_SESSION[namacabang]";?></em></h1>
			<h2></h2>
		     
			 <div id="content">
				<?php  if ($sign==false) { ?>
					<h3>Pencatatan Transaksi Pembelian</h3> <br/><br/>
					<b>Cabang: </b><?php echo " $_SESSION[namacabang]";?>  <br/> <br/>
					<b>Pilihlah pemasok beserta tanggal transaksi yang ingin dicatat transaksinya</b><br/><br/>
					<form id = "form1_catat" name="form_pencatatan" method = "post" action = "pencatatan1.php" >
					<b>Pemasok: </b><select name = "pemasok">
					<?php
						$result = pg_query($dbconn, "select nama from supermarket.pemasok");
						while ( $row = pg_fetch_array($result))
							print("<option>".$row[0]."</option>");
					?>
					</select><br/><br/>
					
					<label><b>Tanggal transaksi :</b></label>
						<SCRIPT>fill_select(document.form_pencatatan); year_install(document.form_pencatatan);</script>
					<br/><br/>
					<label><b>Kode Pembelian: </b></label>
						<input type = "text" name = "kode_beli" size = "5" maxlength = "5">
						<?php echo "$err"; ?><br/><br/>
						<input type="submit" name="submit" value="Submit" /><br/>
					</form> 
				<?php } 
				else{
					echo "<h3>Pencatatan Transaksi Pembelian</h3>
						<br/>";
					
					echo "<form method = 'post' action = 'tampil.php'>
						<b>Produk:</b> 
						<select name = 'produk'>";
					
					$result = pg_query($dbconn, "select nama from supermarket.produk");
					while ( $row = pg_fetch_array($result))
						print("<option>".$row[0]."</option>");
					
					echo " </select><br/><br/>
					Kuantitas: <input type = 'text' name = 'kuantitas' value = '0'/></br><br/><br/>
					<input type='submit' name='pilihan' value='Tambah Transaksi' />
					<input type='submit' name='pilihan' value='Selesai Transaksi' />
					</form>";
				}
				?>
			</div>
		 
			<ul id="nav">
				<li><a href="login_success.php">Home</a></li>
				<?php 
					
					if ($_SESSION['jabatan'] == 'Admin') 
					echo "<li><a href='form_akun.php'>Buat akun</a></li>";
				?>
				<li><a href="q1.php">List Daftar Cabang</a></li>
				<?php 
					if ($_SESSION['jabatan'] == 'Manajer Operasional') {
						echo "<li><a href='pencatatan1.php'>Pencatatan Transaksi Pembelian</a></li>";
					}
				?>
				<li><a href="daftar_transaksi1.php">Daftar Transaksi Pemasok</a></li>
				<li><a href="rekapitulasi1.php">Rekapitulasi Transaksi Pembelian</a></li>
				<li><a href="produk_terlaku.php" >Daftar Produk Terlaku</a></li>
				<li><a href="B10.php" title="View B10 info">About us</a></li>
				<li><a href="logout.php">Logout</a></li>
			</ul>
		
		</div>
	 
		<address>
			&copy; 2008 All Rights Reserved. Designed by <a href="http://www.caddoo.net" title="Portfolio of Matthew Caddoo">Matthew Caddoo</a> for <a href="http://www.zymic.com" title="Zymic Free Templates">Zymic</a> <a href="http://www.zymic.com/free-templates">Free Templates</a>. <a href="http://www.zymic.com/free-web-hosting/">Free Web Hosting</a>.
		</address>

	</body>
</html>
